<?php

namespace Error;

class PhpErrorHandler {
	private static $Types = array (
		E_ERROR => 'E_ERROR',
		E_WARNING => 'E_WARNING',
		E_PARSE => 'E_PARSE',
		E_NOTICE => 'E_NOTICE',
		E_CORE_ERROR => 'E_CORE_ERROR',
		E_CORE_WARNING => 'E_CORE_WARNING',
		E_COMPILE_ERROR => 'E_COMPILE_ERROR',
		E_COMPILE_WARNING => 'E_COMPILE_WARNING',
		E_USER_ERROR => 'E_USER_ERROR',
		E_USER_WARNING => 'E_USER_WARNING',
		E_USER_NOTICE => 'E_USER_NOTICE',
		E_STRICT => 'E_STRICT',
		E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
		E_DEPRECATED => 'E_DEPRECATED',
		E_USER_DEPRECATED => 'E_USER_DEPRECATED',
		E_ALL => 'E_ALL',
	);
	private static $Descriptions = array (
		E_ERROR => '(integer) Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.  ',
		E_WARNING => '(integer) Run-time warnings (non-fatal errors). Execution of the script is not halted.  ',
		E_PARSE => '(integer) Compile-time parse errors. Parse errors should only be generated by the parser.  ',
		E_NOTICE => '(integer) Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.  ',
		E_CORE_ERROR => '(integer) Fatal errors that occur during PHP\'s initial startup. This is like an E_ERROR, except it is generated by the core of PHP.  ',
		E_CORE_WARNING => '(integer) Warnings (non-fatal errors) that occur during PHP\'s initial startup. This is like an E_WARNING, except it is generated by the core of PHP.  ',
		E_COMPILE_ERROR => '(integer) Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.  ',
		E_COMPILE_WARNING => '(integer) Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.  ',
		E_USER_ERROR => '(integer) User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().  ',
		E_USER_WARNING => '(integer) User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().  ',
		E_USER_NOTICE => '(integer) User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().  ',
		E_STRICT => '(integer) Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. Since PHP 5 but not included in E_ALL until PHP 5.4.0',
		E_RECOVERABLE_ERROR => '(integer) Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR. Since PHP 5.2.0',
		E_DEPRECATED => '(integer) Run-time notices. Enable this to receive warnings about code that will not work in future versions. Since PHP 5.3.0',
		E_USER_DEPRECATED => '(integer) User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error(). Since PHP 5.3.0',
		E_ALL => '(integer) All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0. 32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously ',
	);
	
	// int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]]
	public function handle ($errno ,$errstr, $errfile, $errline, $errcontext) {
		$errtype = self::$Types[$errno];
		$errdesc = self::$Descriptions[$errno];

		switch ($errno) {
			case E_NOTICE:
				$color = 34;
				break;
			case E_WARNING:
				$color = 33;
				break;
			default:
				$color = 31;
		}
	
		echo("\x1b[1;{$color}m$errtype\x1b[0m\nErrno: $errno\nErrdesc: $errdesc\nErrstr: $errstr\nErrfile: $errfile\nErrline: $errline\nErrcontext: $errcontext\n");

		debug_print_backtrace();
		
		die();
	}
};


